// map.js
Page({
  data: {
    id: undefined,
    latitude: 40.060539,
    longitude: 116.343847,
    markers: [],
    polyline: [],
    attachment: [],
    houseInfo: '',
    repairInfo: '',
    mobile: '',
    description: '',
    appointment: '',
    status: '',
    location
  },
  onLoad(option) {
    let { id } = option
    this.setData({
      id
    })
    this.getLocation()
    this.RepairDetail(id)
  },
  async getLocation() {
    let { latitude, longitude } = await wx.getLocation({
      type: 'wgs84',
    })
    this.setData({
      latitude,
      longitude
    })
  },
  async RepairDetail(id) {
    let res = await wx.http.get('/repair/' + id)
    console.log(res)
    this.setData({
      attachment: res.attachment,
      appointment: res.appointment,
      description: res.description,
      houseInfo: res.houseInfo,
      mobile: res.mobile,
      repairInfo: res.repairItemName,
      status: res.status
    })
    wx.qqmap.geocoder({
      address: "广州市" + this.data.houseInfo,
      success: res => {
        console.log(res)
        let { lat, lng } = res.result.location
        this.setData({
          markers: [{
            id: 0,
            latitude: lat,
            longitude: lng,
            iconPath: '../../../static/images/marker.png',//图标路径
            width: 20,
            height: 20,
            callout: { //可根据需求是否展示经纬度
              content: res.result.title,
              color: '#000',
              display: 'ALWAYS'
            }
          }]
        })
        this.getDirection()
      },
      fail: function (error) {
        console.error(error);
      }
    })
  },
  getDirection() {
    console.log(this.data.markers)
    wx.qqmap.direction({
      mode: 'bicycling',  // 模式：骑行
      from: {  // 起点，不填为默认定位起点
        latitude: this.data.latitude,
        longitude: this.data.longitude
      },
      to: {
        latitude: this.data.markers[0].latitude,
        longitude: this.data.markers[0].longitude
      },
      success: res => {
        var ret = res;
        var coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        this.setData({
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  goRepair() {
    wx.navigateTo({
      url: '/repair_pkg/pages/form/index?id=' + this.data.id,
    })
  },
  async cancel() {
    if (this.data.status === 0) {
      return wx.showToast({
        title: '已取消',
        icon: 'none'
      })
    }
    await wx.http.put('/cancel/repaire/' + this.data.id)
    wx.showToast({
      title: '取消成功',
      icon: 'success'
    })
    wx.navigateBack()
  }
})
